package main

import (
	"fmt"
	"sync"
	"time"
)

func main() {
	////go lock()
	////go rLock()
	//go wLock()
	time.Sleep(2 * time.Second)
}

// 只会输出 0
func lock() {
	mutex := sync.Mutex{}
	for i := 0; i < 10; i++ {
		mutex.Lock()
		defer mutex.Unlock()
		fmt.Println("lock: ", i)
	}
}

// 读锁
func rLock() {
	mutex := sync.RWMutex{}
	for i := 0; i < 10; i++ {
		mutex.RLock()
		defer mutex.RUnlock()
		fmt.Println("rLock: ", i)
	}
}

// 写锁
func wLock() {
	mutex := sync.RWMutex{}
	for i := 0; i < 10; i++ {
		mutex.Lock()
		defer mutex.Unlock()
		fmt.Println("wLock: ", i)
	}
}
